package com.xyhy.starter.codegen.controller;

import cn.hutool.core.util.TypeUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import com.xyhy.core.base.ToolConstant;
import com.xyhy.core.base.exception.ServiceException;
import com.xyhy.core.web.annotation.NoWrapper;
import com.xyhy.starter.codegen.UiGenerator;

import java.lang.reflect.Type;

/**
 * @author robert
 * @date 2025-06-12
 * @description 前端代码生成 controller
 * 1、Tag枚举，必须添加 getXxxVO方法，如 getTypeVO
 * 2、普通枚举，必须添加 getXxxText方法，如 getTypeText
 */
public abstract class CodegenController<ENTITY> {

  @NoWrapper
  @ApiOperationSupport(order = 1)
  @ApiOperation(value = "AA生成前端UI代码", produces = MediaType.TEXT_PLAIN_VALUE)
  @GetMapping(ToolConstant.URL.GENERATE_UI)
  public String generateUi() {
    return UiGenerator.generator(this.getClass(), getEntityType());
  }

  protected Type getEntityType() {
    return TypeUtil.getTypeMap(this.getClass()).entrySet().stream()
            .filter(x -> x.getKey().toString().equals("ENTITY"))
            .map(x -> x.getValue()).findFirst()
            .orElseThrow(() -> new ServiceException("获取 实体类 失败!"));
  }

}
